package com.example.myapplication.chap07;

import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.R;

import androidx.appcompat.app.AppCompatActivity;

import java.io.UnsupportedEncodingException;

//【例7-3】应用Volley框架从Web服务器读取JSON数据

public class VolleyActivity extends AppCompatActivity implements View.OnClickListener {
    Button Btn;
    TextView txt;

    //String url="http://www.weather.com.cn/data/city3jdata/china.html";//大编号
//    String url="http://www.weather.com.cn/data/city3jdata/provshi/10128.html"; //广东省城市编号
    String url = "http://t.weather.itboy.net/api/weather/city/101280101";//广州天气

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_07_volley);
        Btn = findViewById(R.id.btn);
        txt = findViewById(R.id.txt);
        Btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //1 创建 RequestQueue 对象
        RequestQueue mQueue = Volley.newRequestQueue(this);
        //2 创建 Request 对象，
        StringRequest stringRequest = new StringRequest(
                //第 1个参数，目标服务器的URL地址
                url,
                //第2个参数，服务器响应成功的回调
                new Response.Listener<String>() {  //Volley的监听器
                    @Override
                    public void onResponse(String response) {
                        txt.setText(response);
                    }    //处理传回的数据response
                },
                //第3个参数，服务器响应失败的回调
                new Response.ErrorListener() {      //Volley的监听器
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("TAG", error.getMessage(), error);
                    }
                }) {
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {  //将Volley默认的ISO-8859-1格式转换为utf-8格式
                    String jsonString = new String(response.data, "UTF-8");
                    Log.w("jsonString", jsonString);
                    return Response.success(jsonString, HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
        //3，将 Request 对象添加到 RequestQueue 中，开启排队模式。
        mQueue.add(stringRequest);
    }
}
